package com.ssd.controller;

import com.ssd.common.VMSystem;
import com.ssd.dto.TaskDto;
import com.ssd.entity.Task;
import com.ssd.http.controller.BaseController;
import com.ssd.service.TaskDetailsService;
import com.ssd.service.TaskService;
import com.ssd.utils.DateUtil;
import com.ssd.view.BaseResponse;
import com.ssd.viewmodel.Pager;
import com.ssd.vo.CancelTaskVO;
import com.ssd.vo.SearchTaskVO;
import com.ssd.vo.TaskVO;
import com.ssd.vo.UpdateStatusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


/**
 * 工单Controller
 *
 * @author itheima
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/manage/task")
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskDetailsService taskDetailsService;

    /**
     * 搜索订单
     *
     * @param isRepair
     * @param pageSize
     * @param pageIndex
     * @param taskStatus
     * @param taskCode
     * @param productTypeId
     * @return
     */
    @GetMapping("/search")
    public BaseResponse<Pager<TaskVO>> search(
            @RequestParam(value = "params[isRepair]") Integer isRepair,//工单类型：0.运维工单，1.运营工单
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "taskStatus", required = false) Integer taskStatus,
            @RequestParam(value = "taskCode", required = false) String taskCode,
            @RequestParam(value = "productTypeId", required = false) Integer productTypeId) {
        Pager<TaskVO> taskPager = null;
        List<Integer> productTypeIdList = new ArrayList<>();//要查询的工单类型列表
        //判断查询的工单类型
        if (isRepair == 1) {
            productTypeIdList.add(VMSystem.TASK_TYPE_SUPPLY);
        } else if (isRepair == 0) {
            productTypeIdList.add(VMSystem.TASK_TYPE_DEPLOY);
            productTypeIdList.add(VMSystem.TASK_TYPE_REPAIR);
            productTypeIdList.add(VMSystem.TASK_TYPE_REVOKE);
        }
        //查询列表
        taskPager = taskService.search(productTypeIdList, productTypeId, taskStatus, taskCode, pageIndex, pageSize);
        return new BaseResponse<Pager<TaskVO>>(taskPager);
    }

    /**
     * 获取工单详情
     *
     * @param taskId
     * @return
     */
    @GetMapping("/{taskId}")
    public BaseResponse<Task> getByTaskId(@PathVariable Long taskId) {
        Task task = taskService.getByTaskId(taskId);
        return new BaseResponse<>(task);
    }

    @PostMapping
    public BaseResponse create(@RequestBody TaskDto taskDto) {
        Long taskId = taskService.createTask(taskDto);//创建工单
        //判断是否为补货工单
        if (taskDto.getDetails() != null && taskDto.getDetails().size() > 0) {
            //创建工单详细信息
            boolean saved = taskDetailsService.createTaskDetails(taskId, taskDto.getDetails());
            return new BaseResponse(saved);
        }
        return new BaseResponse(taskId != null);
    }

    /**
     * 获取一个月内的工单数量
     *
     * @return
     */
    @GetMapping("/count")
    public BaseResponse getTotalTask(String start, String end,
                                     @RequestParam(required = false) Integer status) {
        //转换日期类型
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        Integer totalTask = taskService.getTotalTask(startTime, endTime, status);
        return new BaseResponse(totalTask);
    }

    /**
     * 取消工单
     * @param cancelTaskVO
     * @return
     */
    @PutMapping("/cancel")
    public BaseResponse cancel(@RequestBody CancelTaskVO cancelTaskVO){
        return new BaseResponse(taskService.cancel(cancelTaskVO));
    }

    /**
     * 修改工单状态
     * @param updateStatusVO
     * @return
     */
    @PutMapping("/updateStatus")
    public BaseResponse updateType(@RequestBody UpdateStatusVO updateStatusVO){
        return new BaseResponse(taskService.updateStatus(updateStatusVO));
    }
}